package com.openlogic.flow.controller;

import org.dromara.warm.flow.orm.entity.FlowForm;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Path;
import org.noear.solon.annotation.Post;
import org.noear.solon.core.handle.Result;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.validation.annotation.Valid;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.LambdaGetter;
import com.openlogic.common.core.enums.BusinessType;
import com.openlogic.common.core.web.controller.BaseController;
import com.openlogic.common.core.web.domain.PageData;
import com.openlogic.common.datasource.annotation.QzLog;
import com.openlogic.flow.service.IFlowFormService;

/**
 * @ClassName: FlowFormController
 * @Description:  流程表单 控制层
 * @author weiyongpeng
 * @date 2025年2月13日 下午8:03:24
 */
@Valid
@Controller
@Mapping("flow/form")
public class FlowFormController extends BaseController {

	@Inject
	private IFlowFormService formService;

	/**
	 * 表单列表
	 *
	 * @param FlowForm 携带的查询参数
	 * @return {@link TableDataInfo}
	 * @author liangli
	 * @date 2024/9/5 15:07
	 **/
	@Get
	@Mapping(value = "list")
	public PageData<FlowForm> list(Page<FlowForm> page, FlowForm flowForm) {
		QueryWrapper wrapper = QueryWrapper.create(flowForm).orderBy(FlowForm::getCreateTime, false);
		Page<FlowForm> listPage = formService.page(page, wrapper);
		return getPageData(listPage);
	}

	/**
	 * 已发布表单列表
	 *
	 * @param FlowForm 携带的查询参数
	 * @return {@link TableDataInfo}
	 * @author vanlin
	 * @date 2024/11/11 15:07
	 **/
	@Get
	@Mapping(value = "publishedList")
	public PageData<FlowForm> publishedList(Page<FlowForm> page, FlowForm FlowForm) {
		FlowForm.setIsPublish(1); // 已发布
		QueryWrapper wrapper = QueryWrapper.create(FlowForm).orderBy(new LambdaGetter<FlowForm>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Object get(org.dromara.warm.flow.orm.entity.FlowForm source) {
				return source.getCreateTime();
			}
		}, false);
		Page<FlowForm> listPage = formService.page(page, wrapper);
		return getPageData(listPage);
	}

//	/**
//	 * 查询表单详情
//	 *
//	 * @param id 表单ID
//	 * @return {@link Result< FlowForm>}
//	 * @author liangli
//	 * @date 2024/9/5 15:41
//	 **/
//	@Get
//	@Mapping(value = "/{id}")
//	public Result<FlowForm> getForm(@Path("id") Long id) {
//		return Result.succeed(formService.getById(id));
//	}
//
//	/**
//	 * 提交表单
//	 *
//	 * @param FlowForm 表单ID对应的表单内容
//	 * @return {@link Result<Boolean>}
//	 * @author liangli
//	 * @date 2024/9/5 15:07
//	 **/
//	@PreAuthorize("@ss.hasPermi('flow:FlowForm:add')")
//	@QzLog(title = "提交表单", businessType = BusinessType.INSERT)
//	@PostMapping("/saveFormContent")
//	@Transactional(rollbackFor = Exception.class)
//	public Result<Boolean> saveFormContent(@Body Form form) {
//		Form entity = FlowEngine.newForm().setFormContent(form.getFormContent()).setId(form.getId());
//		return Result.succeed(
//				formService.updateById();
//	}
//
	/**
	 * 提交表单
	 *
	 * @param FlowForm 携带的查询参数
	 * @return {@link Result<Boolean>}
	 * @author liangli
	 * @date 2024/9/5 15:07
	 **/
//	@PreAuthorize("@ss.hasPermi('flow:FlowForm:add')")
	@QzLog(title = "提交表单", businessType = BusinessType.INSERT)
	@Post
	@Mapping("/add")
	@Tran
	public Result<Boolean> add(@Body FlowForm FlowForm) {
		return Result.succeed(formService.save(FlowForm));
	}

	/**
	 * 发布表单
	 *
	 * @param id 表单ID
	 * @return {@link Result<Boolean>}
	 * @author liangli
	 * @date 2024/9/5 15:26
	 **/
//	@PreAuthorize("@ss.hasPermi('flow:FlowForm:publish')")
	@QzLog(title = "发布表单", businessType = BusinessType.INSERT)
	@Get
	@Mapping("publish/{id}")
	@Tran
	public Result<Boolean> publish(@Path("id") Long id) {
		return Result.succeed(formService.publish(id));
	}
//
//	/**
//	 * 取消表单
//	 *
//	 * @param id 表单ID
//	 * @return {@link Result<Boolean>}
//	 * @author liangli
//	 * @date 2024/9/5 15:26
//	 **/
////	@PreAuthorize("@ss.hasPermi('flow:FlowForm:unPublish')")
//	@QzLog(title = "取消发布表单", businessType = BusinessType.INSERT)
//	@Get
//	@Mapping("unPublish/{id}")
//	@Tran
//	public Result<Boolean> unPublish(@Path("id") Long id) {
//		return Result.succeed(formService.unPublish(id));
//	}
//
//	/**
//	 * 修改表单
//	 *
//	 * @param FlowForm 表单
//	 * @return {@link Result<Boolean>}
//	 * @author liangli
//	 * @date 2024/9/5 15:26
//	 **/
////	@PreAuthorize("@ss.hasPermi('flow:FlowForm:edit')")
//	@QzLog(title = "流程定义", businessType = BusinessType.UPDATE)
//	@Put
//	@Mapping("edit")
//	@Tran
//	public Result<Boolean> edit(@Body FlowForm FlowForm) {
//		return Result.succeed(formService.updateById(FlowForm));
//	}
//
//	/**
//	 * 删除表单
//	 *
//	 * @param ids 表单ID
//	 * @return {@link Result<Boolean>}
//	 * @author liangli
//	 * @date 2024/9/5 16:19
//	 **/
////	@PreAuthorize("@ss.hasPermi('flow:FlowForm:remove')")
//	@QzLog(title = "流程定义", businessType = BusinessType.DELETE)
//	@Delete
//	@Mapping("{ids}")
//	@Tran
//	public Result<Boolean> remove(@Path List<Long> ids) {
//		return Result.succeed(formService.removeByIds(ids));
//	}
//
//	/**
//	 * 复制表单
//	 *
//	 * @param id 表单ID
//	 * @return {@link Result<Boolean>}
//	 * @author liangli
//	 * @date 2024/9/5 16:19
//	 **/
////	@PreAuthorize("@ss.hasPermi('flow:FlowForm:add')")
//	@QzLog(title = "流程定义", businessType = BusinessType.INSERT)
//	@Post
//	@Mapping("copyForm/{id}")
//	@Tran
//	public Result<Boolean> copyForm(@Path("id") Long id) {
//		return Result.succeed(formService.copyForm(id));
//	}
}
